package com.wharf.base.controller;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.wharf.base.interceptor.AuthTool;
import com.wharf.base.model.UserInfo;
import com.wharf.base.service.GoodsCarService;
import com.wharf.base.service.UserInfoService;

@RestController
@RequestMapping("/back")
public class LoginContrroller extends BaseController {
	public Integer i;
	@Autowired
	private UserInfoService userInfoService;
	@Autowired
	private GoodsCarService goodscarservice;
	/**管理员登录*/
	@GetMapping("/adlogin")
	public boolean Adminlogin(String user,String pass) {
		boolean loginAction = userInfoService.AdminLoginAction(user, pass);
		return loginAction;
	}
	
	//普通用户登录
	@RequestMapping(value = "/userlogin", method = RequestMethod.POST)
	public String selectUserName(@RequestBody UserInfo user) {
		//获取用户登录id
//		 Integer userId=user.getUserId();
//		 i=userId;		
		String userName = user.getInfoName();
		String userPassword = user.getInfoPass();
		
		String result = "-1";
		UserInfo userinfo=userInfoService.selectByIdandpass(userName, userPassword);
		if (userInfoService.selectByIdandpass(userName, userPassword) == null) {
			// return "用户不存在或输入错误";
			result = "0";
			
		} else {			
			session.setAttribute(LOGIN_USER,userinfo);
			super.session.setAttribute(AuthTool.LOGIN_STATUS, userName);
			result = "1";
//  	  return "成功登录";			
		}
		return result;
	}
	
	
	//注册用户    1；在user用户表里添加用户  2；在购物车里初始化一条信息
	 @RequestMapping(value = "/addUser", method = RequestMethod.POST)
	    public int addUser(@RequestBody UserInfo user) {
	       
		 
		 	String infoName = user.getInfoName();
	       //得到用户id
	        //Integer userId=user.getUserId();
	        //在用户注册的时候初始化一条购物车表信息
	        
	       //goodscarservice.addCar(userId);
	        
	        String infoPhone = user.getInfoPhone();
	        String infoQq = user.getinfoQq();
	        String infoPass = user.getInfoPass();	
	        return userInfoService.regist(infoName, infoPhone, infoQq, infoPass);
	    }

}
